<?php

namespace App\Common\ScarecrowProvider;

use App\Models\BackHome\AuthManage\LoginModel as BackHomeLoginModel;
use App\Models\AppHome\AuthManage\LoginModel as AppHomeLoginModel;
use App\Models\MiniHome\AuthManage\LoginModel as MiniHomeLoginModel;
use App\Models\WeChatOauthHome\AuthManage\LoginPcModel as WeChatOauthHomeLoginPcModel;

/**
 * Scarecrow自己做的Auth管理器
 * Class ScarecrowAuthProvider
 * @package App\Common\ScarecrowProvider
 */
class ScarecrowAuthProvider
{
    protected static $LoginModel;

    public function __construct()
    {
        switch (APP_USER_TOKEN_SRC) {
            case 1:
                self::$LoginModel = BackHomeLoginModel::class;
                break;
            case 2:
                self::$LoginModel = AppHomeLoginModel::class;
                break;
            case 3:
                self::$LoginModel = MiniHomeLoginModel::class;
                break;
            case 4:
                self::$LoginModel = WeChatOauthHomeLoginPcModel::class;
                break;
            default:
                self::$LoginModel = BackHomeLoginModel::class;
        }
    }

    /**
	 * 获取当前用户ID
	 * @param string $token
	 * @return bool|mixed
	 */
	public function id($token = '')
	{
		$userInfo = $this->user($token);
		if ($userInfo) {
			return $userInfo['id'];
		}
		return false;
	}

	/**
	 * 获取用户信息
	 * @param string $token
	 * @return bool
	 */
	public function user($token = '')
	{
		$token = $token ?: (defined('APP_USER_TOKEN') ? APP_USER_TOKEN : "");
		$userInfoObj = self::$LoginModel::getUserInfo($token);
		if ($userInfoObj['code'] == RETURN_SUCCESS) {
			return $userInfoObj['data'];
		}
		return false;
	}

	/**
	 * 退出登录
	 * @param string $token
	 * @return bool
	 */
	public function logout($token = '')
	{
		$token = $token ?: (defined('APP_USER_TOKEN') ? APP_USER_TOKEN : "");
        self::$LoginModel::LoginOut($token);
		return true;
	}


	/**
	 * 判断用户是否是超级管理员
	 * @param string $token
	 * @return bool
	 */
	public function isAdmin($token = '')
	{
		$token = $token ?: (defined('APP_USER_TOKEN') ? APP_USER_TOKEN : "");
		$userInfoObj = self::$LoginModel::getUserInfo($token, 'is_supper_admin');
		if ($userInfoObj['code'] == RETURN_SUCCESS) {
			return (bool)$userInfoObj['data'];
		}
		return false;
	}

    public function __call($name, $arguments)
    {
        // TODO: Implement __call() method.
        return self::$LoginModel::$name(...$arguments);
    }

    public function getModelObject() {
	    return self::$LoginModel;
    }
}
